import time

from Common.Base import Base, open_browser

commodity_url = "http://localhost:8080/ecshop/admin/index.php"


class CommodityPage(Base):
    close_com = ("css selector", "span[onclick='btnCancel(this)']")  # 点击x关闭弹窗
    login_com = ("css selector", "span[class='btn-tab']")  # 点击eshop登录
    user_com = ("css selector", "input[name='username']")  # 点击用户名
    password_com = ("css selector", "input[name='password']")  # 点击用户密码
    sign_com = ("css selector", "input[value='登 录']")  # 点击登录
    goods_com = ("css selector", "li[class='icon-goods']")  # 点击商品管理 id="menu-frame"
    shop_com = ("css selector", "a[href='goods.php?act=add']")  # 添加新商品
    general_com = ("css selector", "span[id='general-tab']")  # 点击通用信息
    detail_com = ("css selector", "span[id='detail-tab']")  # 点击详细描述
    mix_com = ("css selector", "span[id='mix-tab']")  # 点击其他信息
    properties_com = ("css selector", "span[id='properties-tab']")  # 点击商品属性
    gallery_com = ("css selector", "span[id='gallery-tab']")  # 点击商品相册
    goods_name_com = ("css selector", "input[name='goods_name']")  # 点击商品名称
    style_com = ("css selector", "select[name='goods_name_style']")  # 字体样式下拉框
    hide_com = ("css selector", "select[name='cat_id']")  # 商品分类下拉框
    price_com = ("css selector", "input[name='shop_price']")  # 本店售价
    btn_com = ("css selector", "input[class='btn btn-def']")  # 点击按市场价计算
    button_com = ("css selector", "input[value=' 确定 ']")  # 点击确定
    frame_index_shop = ("css selector", "frame[src='index.php?act=menu']")  # 商品管理frame定位器
    frame_goods_com = ("css selector", "frame[src='goods.php?act=list']")  # 添加商品frame定位器

    def close_pop(self):
        """
        点击X关闭弹窗的方法
        :return:
        """
        self.click(self.close_com)

    def login_eshop(self):
        """
        点击ecshop登录的方法
        :return:
        """
        self.click(self.login_com)

    def user_send(self, username):
        """
        输入用户名的方法
        :param username: 输入用户名
        :return:
        """
        self.send_keys(self.user_com, username)

    def password_send(self, password):
        """
        输入密码的方法
        :param password: 输入密码
        :return:
        """
        self.send_keys(self.password_com, password)

    def sign_click(self):
        """
        点击登录的方法
        :return:
        """
        self.click(self.sign_com)

    def goods_click(self):
        """
        点击商品管理的方法
        :return:
        """
        self.frame_open(self.frame_index_shop)
        self.click(self.goods_com)
        self.frame_off()

    def shop_click(self):
        """
        点击添加商品方法
        :return:
        """
        self.frame_open(self.frame_index_shop)
        self.click(self.shop_com)
        self.frame_off()

    def general_click(self):
        """
        点击通用信息方法
        :return:
        """
        self.frame_open(self.frame_goods_com)
        self.click(self.general_com)

    def goods_name_send(self, text):
        """
        输入商品名称
        :return:
        """
        self.send_keys(self.goods_name_com, text)

    def style_click(self, text):
        """
        下拉框选择
        :param text:
        :return:
        """
        self.select_dropdown(self.style_com, text)

    def hide_click(self, text):
        """
        商品分类选择
        :param text:
        :return:
        """
        self.select_dropdown(self.hide_com, text)

    def price_send(self, text):
        """
        输入售价
        :return:
        """
        self.send_keys(self.price_com, text)

    def btn_click(self):
        """
        点击按市场价计算
        :return:
        """
        self.click(self.btn_com)

    def button_click(self):
        """
        点击确定
        :return:
        """
        self.click(self.button_com)


if __name__ == '__main__':
    driver = open_browser()
    driver.maximize_window()
    cod = CommodityPage(driver)
    cod.open_url(commodity_url)
    cod.close_pop()
    cod.login_eshop()
    cod.user_send("admin")
    cod.password_send("Aa112211")
    cod.sign_click()
    cod.goods_click()
    cod.shop_click()
    cod.general_click()
    cod.goods_name_send("外星飞船")
    cod.style_click("加粗")
    cod.hide_click("智能硬件")
    cod.price_send("520")
    cod.btn_click()
    cod.button_click()
    time.sleep(20)
    cod.close_browser()
